#include"process.h"
#include<unistd.h>
#include<time.h>
#include<stdlib.h>
//定义一个函数指针类型
typedef void (*call_t)(const char*,double,double);//定义了一个函数指针的类型

double total=1024.0;//一共要下载的总量
//double speed=1.0;//定义一个网速
double speed[]={1.0,0.5,1.2,0.01,0.02};//定义一个网速清单

//下载
//回调函数
void download(int total,call_t cb)
{
  srand(time(NULL));
  double current=0.0;
  while(current<=total)
  {
    //更新进度
    cb("下载",total,current);//进行回调
    if(current>=total) break;
    //下载代码
    int random=rand()%5;
    usleep(50000);

    current+=speed[random];
    if(current>=total)
    {
      current=total;
    }
  }//加上随机数进行动态的调整操作
}
void uploadload(int total,call_t cb)
{
  srand(time(NULL));
  double current=0.0;
  while(current<=total)
  {
    //更新进度
    cb("上传中",total,current);//进行回调
    if(current>=total) break;
    //下载代码
    int random=rand()%5;
    usleep(50000);

    current+=speed[random];
    if(current>=total)
    {
      current=total;
    }
  }//加上随机数进行动态的调整操作
}
int main()
{
  download(1024.0,FlushProcess);
  printf("download 1024.0MB done\n");
  download(512.0,FlushProcess);
  printf("download 512.0MB done\n");
  download(334.0,FlushProcess);
  printf("download 334.0MB done\n");
  //process();
  download(64.0,FlushProcess);
  printf("download 64.0MB done\n");
  return 0;
}
